/**
* <p>Title: HelloServerAioHandler.java</p>
* <p>Description: </p>
* <p>Copyright: </p>
* <p>Company: </p>
* @author du
* @date 2018年3月7日
* @version 1.0
*/   
package org.ds.streamserver;

import java.nio.ByteBuffer;

import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.core.GroupContext;
import org.tio.core.exception.AioDecodeException;
import org.tio.core.intf.Packet;
import org.tio.server.intf.ServerAioHandler;

/**  
* 创建时间：2018年3月7日 下午10:42:51  
* 项目名称：ds-sys-streamserver  
* @author du
* @version 1.0   
* @since JDK 1.7
* 文件名称：HelloServerAioHandler.java  
* 类说明：  
*/
/**
* <p>Title: HelloServerAioHandler</p>
* <p>Description: </p>
* <p>Company: </p> 
* @author du
* @date 2018年3月7日
*/
public class HelloServerAioHandler implements ServerAioHandler{

	/* （非 Javadoc）
	* <p>Title: decode</p>
	* <p>Description: </p>
	* @param buffer
	* @param channelContext
	* @return
	* @throws AioDecodeException
	* @see org.tio.core.intf.AioHandler#decode(java.nio.ByteBuffer, org.tio.core.ChannelContext)
	*/
	@Override
	public Packet decode(ByteBuffer buffer, ChannelContext channelContext) throws AioDecodeException {
		int readableLength = buffer.limit() - buffer.position();
		//收到的数据组不了业务包，则返回null以告诉框架数据不够
		if (readableLength < HelloPacket.HEADER_LENGHT) {
			System.out.println("收到的数据组不了业务包");
			return null;
		}

		//读取消息体的长度
		int bodyLength = buffer.getInt();

		//数据不正确，则抛出AioDecodeException异常
		if (bodyLength < 0) {
			throw new AioDecodeException("bodyLength [" + bodyLength + "] is not right, remote:" + channelContext.getClientNode());
		}

		//计算本次需要的数据长度
		int neededLength = HelloPacket.HEADER_LENGHT + bodyLength;
		//收到的数据是否足够组包
		int isDataEnough = readableLength - neededLength;
		// 不够消息体长度(剩下的buffe组不了消息体)
		if (isDataEnough < 0) {
			return null;
		} else //组包成功
		{
			HelloPacket imPacket = new HelloPacket();
			if (bodyLength > 0) {
				byte[] dst = new byte[bodyLength];
				buffer.get(dst);
				imPacket.setBody(dst);
			}
			return imPacket;
		}
	}

	/* （非 Javadoc）
	* <p>Title: encode</p>
	* <p>Description: </p>
	* @param packet
	* @param groupContext
	* @param channelContext
	* @return
	* @see org.tio.core.intf.AioHandler#encode(org.tio.core.intf.Packet, org.tio.core.GroupContext, org.tio.core.ChannelContext)
	*/
	@Override
	public ByteBuffer encode(Packet packet, GroupContext groupContext, ChannelContext channelContext) {
		HelloPacket helloPacket = (HelloPacket) packet;
		byte[] body = helloPacket.getBody();
		int bodyLen = 0;
		if (body != null) {
			bodyLen = body.length;
		}

		//bytebuffer的总长度是 = 消息头的长度 + 消息体的长度
		int allLen = HelloPacket.HEADER_LENGHT + bodyLen;
		//创建一个新的bytebuffer
		ByteBuffer buffer = ByteBuffer.allocate(allLen);
		//设置字节序
		buffer.order(groupContext.getByteOrder());

		//写入消息头----消息头的内容就是消息体的长度
		buffer.putInt(bodyLen);

		//写入消息体
		if (body != null) {
			buffer.put(body);
		}
		return buffer;
	}

	/* （非 Javadoc）
	* <p>Title: handler</p>
	* <p>Description: </p>
	* @param packet
	* @param channelContext
	* @throws Exception
	* @see org.tio.core.intf.AioHandler#handler(org.tio.core.intf.Packet, org.tio.core.ChannelContext)
	*/
	@Override
	public void handler(Packet packet, ChannelContext channelContext) throws Exception {
		HelloPacket helloPacket = (HelloPacket) packet;
		byte[] body = helloPacket.getBody();
		if (body != null) {
			String str = new String(body, HelloPacket.CHARSET);
			System.out.println("收到消息：" + str);

			HelloPacket resppacket = new HelloPacket();
			resppacket.setBody(("收到了你的消息，你的消息是:" + str).getBytes(HelloPacket.CHARSET));
			Aio.send(channelContext, resppacket);
		}
		return;
		
	}

}
 